package org.bookie.framework.components;

import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.bookie.framework.CommonBaseComponent;
import org.bookie.model.Period;

/**
 * Component modelling a period. 
 */
public abstract class PeriodComponent extends CommonBaseComponent implements PageBeginRenderListener{
	
	/**
	 * @see org.apache.tapestry.event.PageBeginRenderListener#pageBeginRender(org.apache.tapestry.event.PageEvent)
	 */
	public void pageBeginRender(PageEvent event) {
		if(null==getPeriod()){
			setPeriod(new Period());
		}
	}

	/**
	 * Get the {@link Period}
	 * @return instance of Period
	 */
	public abstract Period getPeriod();
	
	/**
	 * Set the {@link Period}
	 * @param period Period to set
	 */
	public abstract void setPeriod(Period period);
	
	/**
	 * Get the component to be updated after a new period was set.
	 * @return client identifier of the component
	 */
	public abstract String getListener();
	/**
	 * Set the component to be updated after a new period was set.
	 * @param id client identifier of the component
	 */
	public abstract void setListener(String id);
	
	/**
	 * Listener method invoked when the period form is submitted
	 */
	public void addPeriod(){
		Period period = getPeriod();
		if(period.getEnd()!=null && period.getStart()!=null){
			if(!period.isValid()){
				recordError("form.error.inconsistency");
			}
		}
		if(getListener()!=null){
			getResponseBuilder().updateComponent(getListener());
		}
	}
}
